﻿@page "/RecordReviewHistory"
@using System.ComponentModel.DataAnnotations
@using System.ComponentModel
@using System.Text.Json
@using AntDesign.TableModels
@layout TeacherLayout
@inject TeacherClient Http
@inject NavigationManager Navigation
@inject IJSRuntime JS

<PageTitle>已审核列表</PageTitle>

@if (@loading)
{
    <p>Loading...</p>

}
else
{
    <div style="margin:0 0 10px 5px;">
        <Button Loading=@_downloading Type=@ButtonType.Primary @onclick="DownloadUnfinishedStudents">
            导出未完成学生列表
        </Button>
        <Button Loading=@_downloading Type=@ButtonType.Primary @onclick="DownloadFinishedStudents">
            导出已完成学生信息
        </Button>
    </div>
    <Table @ref="table"
        TItem="RecordReviewHistoryFront"
        DataSource="@reviewHistoriesFront"
        Total="_total"
        @bind-PageIndex="_pageIndex"
        @bind-PageSize="_pageSize"
        @bind-SelectedRows="selectedRows">
        <Selection Key="@(context.Id.ToString())" />
        <Column @bind-Field="@context.Sno" Sortable />
        <Column @bind-Field="@context.Time" Format="yyyy-MM-dd" Sortable />
        <Column Title="学生名字" Field="@context.Name" />
        <Column Title="旧宿舍" Field="@context.OldDoom"/>
        <Column Title="新宿舍" Field="@context.NewDoom"/>
        <Column Title="旧校外地址" Field="@context.OldAddress"/>
        <Column Title="新校外地址" Field="@context.NewAddress"/>
        <Column Title="状态" Field="@context.Status"/>
        <Column Title="原因备注" Field="@context.Comment"/>
    </Table>
}



@code {
    ITable? table;
    private bool loading = true;
    IList<RecordReviewHistory>? reviewHistories;
    List<RecordReviewHistoryFront>? reviewHistoriesFront;
    IEnumerable<RecordReviewHistoryFront>? selectedRows;

    int _pageIndex = 1;
    int _pageSize = 10;
    int _total = 0;

    protected async override Task OnInitializedAsync()
    {
        reviewHistories = await Http.GetReviewHistory();

        reviewHistoriesFront = reviewHistories.Select(x => new RecordReviewHistoryFront
                                        {
                                            Id = x.Id,
                                            Time = x.Time,
                                            Sno = x.Sno,
                                            Name = x.Name,
                                            OldDoom = x.OldDoom,
                                            NewDoom = x.NewDoom,
                                            OldAddress = x.OldAddress,
                                            NewAddress = x.NewAddress,
                                            Comment = x.Comment,
                                            Status = x.Status
                                        })
                              .ToList();
        _total = reviewHistoriesFront.Count;
        loading = false;
    }
    public class RecordReviewHistoryFront
    {
        public int Id { get; set; }

        [DisplayName("日期")]
        public DateTime Time { get; set; }

        [DisplayName("学号")]
        public string Sno { get; set; } = null!;

        [DisplayName("学生名字")]
        public string Name { get; set; } = null!;

        [DisplayName("原宿舍")]
        public string? OldDoom { get; set; } // 例如：学海2舍-302  西14-321

        [DisplayName("修改后宿舍")]
        public string? NewDoom { get; set; }

        [DisplayName("原校外住宿地址")]
        public string? OldAddress { get; set; } 

        [DisplayName("修改后校外住宿地址")]
        public string? NewAddress { get; set; }

        [DisplayName("状态")]
        public string? Status { get; set; }

        [DisplayName("备注")]
        public string? Comment{ get; set; }
    }

    bool _downloading = false;

    public async Task DownloadUnfinishedStudents()
    {
        _downloading = true;
        var stream = await Http.GetUnfinishedStudents();
        var fileName = $"未完成学生列表{DateTime.Now:MMddHHmm}.xlsx";
        using var streamRef = new DotNetStreamReference(stream: stream);

        await JS.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
        _downloading = false;
    }

    public async Task DownloadFinishedStudents()
    {
        _downloading = true;
        var stream = await Http.GetFinishedStudents();
        var fileName = $"已核对学生信息{DateTime.Now:MMddHHmm}.xlsx";
        using var streamRef = new DotNetStreamReference(stream: stream);

        await JS.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
        _downloading = false;
    }
}